package com.kuizii.generator.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kuizii.generator.constant.Constants;
import com.kuizii.generator.dao.OracleSysMapper;
import com.kuizii.generator.dao.SqlServerSysMapper;
import com.kuizii.generator.dao.SysMapper;
import com.kuizii.generator.service.SysService;
import com.kuizii.generator.util.generator.*;
import org.apache.commons.configuration.Configuration;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.ByteArrayOutputStream;
import java.util.List;
import java.util.Map;
import java.util.zip.ZipOutputStream;

/**
 * <p>
 *
 * </p>
 *
 * @author nuanyang
 * Email
 * created by 2018/6/13
 */
@Service
public class SysServiceImpl implements SysService {

    @Autowired
    private SysMapper sysMapper;

    @Autowired
    private SqlServerSysMapper sqlServerSysMapper;
    @Autowired
    OracleSysMapper oracleSysMapper;

    @Override
    public List<Map<String, Object>> queryList(Map<String, Object> map) {
        if (isMysql()) {
            return sysMapper.queryList(map);
        } else if (isSqlServer()) {
            return sqlServerSysMapper.queryList(map);
        } else if (isOracle()) {
            return oracleSysMapper.queryList(new Page(Long.parseLong(map.getOrDefault("page", 1).toString()),
                    Long.parseLong(map.getOrDefault("limit", 10).toString())), map);
        } else {
            throw new RuntimeException("请添加数据库的支持类型 如：mysql 或者 sqlserver");
        }
    }

    @Override
    public Integer queryTotal(Map<String, Object> map) {
        if (isMysql()) {
            return sysMapper.queryTotal(map);
        } else if (isSqlServer()) {
            return sqlServerSysMapper.queryTotal(map);
        } else if (isOracle()) {
            return oracleSysMapper.queryTotal(map);
        } else {
            throw new RuntimeException("请添加数据库的支持类型 如：mysql 或者 sqlserver");
        }

    }

    @Override
    public byte[] generatorCode(String[] tableNames) {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        ZipOutputStream zip = new ZipOutputStream(outputStream);
        GeneratorApiUtils.generatorCode(zip);
        GeneratorParentUtils.generatorCode(zip);
        if (Constants.MICRO_SERVICE_TYPE.equals(GeneratorConfig.getPlatformType())) {
            GenteratorStarterUtils.generatorCode(zip);
            GeneratorCoreUtils.generatorOnceCode(zip);
        } else if (Constants.MODULE_TYPE.equals(GeneratorConfig.getPlatformType())) {
            GeneratorCoreUtils.generatorOnceCode(zip);
        } else {
            GenteratorStarterUtils.generatorCode(zip);
            GeneratorWebApiUtils.generatorOnceCode(zip);
            GeneratorCoreUtils.generatorOnceCode(zip);
        }

        //生成与表有关的数据
        for (String tableName : tableNames) {
            //查询表信息
            Map<String, String> table = queryTable(tableName);
            //查询列信息
            List<Map<String, String>> columns = queryColumns(tableName);
            //生成代码
            GeneratorCoreUtils.generatorCode(table, columns, zip);
            if (Constants.PLATFORM_TYPE.equals(GeneratorConfig.getPlatformType())) {
                GeneratorWebApiUtils.generatorCode(table, columns, zip);
            }
            GeneratorApiUtils.generatorCode(table, columns, zip);
        }
        IOUtils.closeQuietly(zip);
        return outputStream.toByteArray();
    }

    /**
     * 查询表
     *
     * @param tableName
     * @return
     */
    public Map<String, String> queryTable(String tableName) {
        if (isMysql()) {
            return sysMapper.queryTable(tableName);
        } else if (isSqlServer()) {
            return sqlServerSysMapper.queryTable(tableName);
        } else if (isOracle()) {
            return oracleSysMapper.queryTable(tableName);
        } else {
            throw new RuntimeException("请添加数据库的支持类型 如：mysql 或者 sqlserver");
        }
    }

    /**
     * 查询列
     *
     * @param tableName
     * @return
     */
    public List<Map<String, String>> queryColumns(String tableName) {
        if (isMysql()) {
            return sysMapper.queryColumns(tableName);
        } else if (isSqlServer()) {
            return sqlServerSysMapper.queryColumns(tableName);
        } else if (isOracle()) {
            return oracleSysMapper.queryColumns(tableName);
        } else {
            throw new RuntimeException("请添加数据库的支持类型 如：mysql 或者 sqlserver");
        }

    }


    /**
     * 判断是不是mysql
     *
     * @return
     */
    public boolean isMysql() {
        Configuration config = GeneratorConfig.getConfig();
        return "mysql".equals(config.getString("sql-type"));
    }

    /**
     * 判断是不是sqlserver
     *
     * @return
     */
    public boolean isSqlServer() {
        Configuration config = GeneratorConfig.getConfig();
        return "sqlserver".equals(config.getString("sql-type"));
    }

    public boolean isOracle() {
        Configuration config = GeneratorConfig.getConfig();
        return "oracle".equals(config.getString("sql-type"));
    }

}
